package org.example.maven.controller;

import jakarta.transaction.Transactional;
import org.example.maven.annotation.ReadOnlyMode;
import org.example.maven.annotation.logAnnotation;
import org.example.maven.common.RedisCommon;
import org.example.maven.pojo.ClassificationTree;
import org.example.maven.pojo.Result;
import org.example.maven.pojo.classification.ClassificationIds;
import org.example.maven.pojo.classification.table.EditClassification;
import org.example.maven.pojo.classification.table.SearchClassification;
import org.example.maven.pojo.utils.addClassification;
import org.example.maven.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/classification")
public class ClassificationController {
    @Autowired
    private ClassificationService classificationserviceImpl;
    @GetMapping
    @PreAuthorize("permitAll()")
    //获取分类树形单选框的分类选项
    public Result getClassification(){
        List<ClassificationTree> list=classificationserviceImpl.getClassification();
        return Result.success(list);
    }
    @GetMapping("/getClassification")
    @PreAuthorize("permitAll()")
    //根据分类编号获取分类的id
    public Result getSpecialClassification(@RequestParam(name = "subId",required = false) String subId){
        return Result.success(classificationserviceImpl.getSpecificClassification(subId));
    }
    /*
    @GetMapping("/getClassificationListTable")
    public Result getClassificationListTable(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        return Result.success(classificationserviceImpl.getClassificationListTable(pageNum,pageSize));
    }*/

    @GetMapping("/getClassificationListTable")
    @PreAuthorize("permitAll()")
    //获取分类管理的展示分类信息
    public Result getClassificationListTableTest(@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize) {
        return Result.success(classificationserviceImpl.getClassificationListTableTest(pageNum,pageSize));
    }

    @GetMapping("/getClassificationListCount")
    //获取分类的总数
    @PreAuthorize("permitAll()")
    public Result getClassificationListCount(){
        return Result.success(classificationserviceImpl.getClassificationListCount(new SearchClassification()));
    }

    @GetMapping("/getSpecieClassification")
    //获取特定的分类
    @PreAuthorize("permitAll()")
    public Result getSpecieClassification(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, SearchClassification searchClassification){
        Result res=Result.success("查找成功");
        System.out.println(searchClassification);
        res.put("data",classificationserviceImpl.getSpecieClassification(pageNum,pageSize,searchClassification));
        res.put("total",classificationserviceImpl.getClassificationListCount(searchClassification));
        return res;
    }

    @GetMapping("/getEditClassification")
    //获取分类的编辑信息
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result getEditClassification(@RequestParam("id") String id){
        return Result.success(classificationserviceImpl.getEditClassification(id));
    }

    @PatchMapping
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "停用分类"
    )
    //停用一级分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result updateClassificationIsDeactivate(@RequestBody ClassificationIds classificationIds){
        classificationserviceImpl.updateClassificationIsDeactivate(classificationIds.getId());
        return Result.success();
    }

    @PatchMapping("/setClassificationIsDeactivate")
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "启用分类"
    )
    //启用一级分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result setClassificationIsDeactivate1(@RequestBody ClassificationIds classificationIds){
        classificationserviceImpl.setClassificationIsDeactivate1(classificationIds.getId());
        return Result.success();
    }

    @PatchMapping("/setClassificationSubcategoryIsDeactivate0")
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "停用分类"
    )
    //停用二级分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result setClassificationSubcategory0(@RequestBody ClassificationIds classificationIds){
        classificationserviceImpl.setClassificationSubcategory0(classificationIds.getId());
        return Result.success();
    }

    @PatchMapping("/setClassificationSubcategoryIsDeactivate1")
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "启用分类"
    )
    //启用二级分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result setClassificationSubcategory1(@RequestBody ClassificationIds classificationIds){
        classificationserviceImpl.setClassificationSubcategory1(classificationIds.getId());
        return Result.success();
    }

    @PatchMapping("/updateCategory")
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "修改分类{#editClassification.category}"
    )
    //更新一级分类信息
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result updateCategory(@RequestBody EditClassification editClassification){
        classificationserviceImpl.updateClassificationCategory(editClassification);
        return Result.success();
    }

    @PatchMapping("/updateSubcategory")
    @ReadOnlyMode
    @logAnnotation(
            type = "更新",
            detail = "修改分类{#editClassification.category}"
    )
    //更新二级分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result updateSubcategory(@RequestBody EditClassification editClassification){
        classificationserviceImpl.updateClassificationSubcategory(editClassification);
        return Result.success();
    }

    @GetMapping("/getClassificationCategory")
    //获取一级分类单选框的分类选项
    @PreAuthorize("permitAll()")
    public Result getClassificationCategory(){
        return Result.success(classificationserviceImpl.getClassificationCategory());
    }

    @PostMapping
    @ReadOnlyMode
    @Transactional
    @logAnnotation(
            type = "新增",
            detail = "新增分类{#addClassification.classificationCategoryAdd.category}"
    )
    //添加分类
    @PreAuthorize("hasAnyAuthority('classification:edit')")
    public Result addClassification(@RequestBody addClassification addClassification){
        System.out.println(addClassification);
        if(addClassification.getClassificationCategoryAdd().getCategoryId()==null){
            classificationserviceImpl.addClassificationCategory(addClassification.getClassificationCategoryAdd());
            addClassification.getClassificationSubcategoryAdd().setCategoryId(addClassification.getClassificationCategoryAdd().getCategoryId());
            // 再次判空
            if (addClassification.getClassificationSubcategoryAdd().getCategoryId() == null) {
                throw new RuntimeException("一级分类ID生成失败");
            }else {
                System.out.println("生成的一级分类ID: " +addClassification.getClassificationSubcategoryAdd().getCategoryId() );  // 此处应为非空值
            }
            addClassification.getClassificationSubcategoryAdd().setSubcategoryInterval(addClassification.getClassificationCategoryAdd().getCategoryId()+String.valueOf(Math.random()));
            classificationserviceImpl.addClassificationSubcategory(addClassification.getClassificationSubcategoryAdd());
        }
        else {
            addClassification.getClassificationSubcategoryAdd().setSubcategoryInterval(addClassification.getClassificationCategoryAdd().getCategoryId()+String.valueOf(Math.random()));
            classificationserviceImpl.addClassificationSubcategory(addClassification.getClassificationSubcategoryAdd());
        }
        return Result.success();
    }

    //获取数据面板的分类信息
    @GetMapping("/getCategoryEcharts")
    public Result getCategoryEcharts(){
        return Result.success(classificationserviceImpl.getCategoryEcharts());
    }

}
